<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index() {

        $per_page = getGP('per_page') ? getGP('per_page') : 1;
        $start = ($per_page - 1) * $this->pagination->per_page;
        $limit = $this->pagination->per_page;

        $keyword = getGP('keyword');
        $data['keyword'] = $keyword;
        $where = " where 1 and (user_type='1' or user_type='2')";
        if ($data['keyword']) {
            $where .=" and (username like '%$keyword%' or account like '%$keyword%')";
        }
        $data['menu'] = "user_user";
        $sql = "select * from user $where ";
        // $sql="select t.* from permission_account as p ,permission_account as t where p.user_id='".$_SESSION['account']."' and p.token_id=t.id";
        $count = fetch_count($sql);
        $res = fetch_array($sql . " order by id desc  limit $start,$limit");
        // $res=fetch_array($sql);
        $data['res'] = $res;

        $config['total_rows'] = $count;
        $config['base_url'] = "index.php?c=user";
        $this->pagination->initialize($config);
        $page = $this->pagination->create_links();
        $data['pager'] = $page;

        $this->load->view('user_list', $data);
    }

    public function add() {
        $data['menu'] = "user_user";
        $sql = "select * from token where 1 order by id asc ";
        $restoken = fetch_array($sql);
        $data['tokendata'] = $restoken;
        $this->load->view('user_add', $data);
    }

    public function edit() {
        $data['menu'] = "user_user";
        $userid = getGP('id');
        $sql = "select * from user where id='" . $userid . "'";
        $res = fetch_one_array($sql);
        $data['res'] = $res;
        $sql = "select * from token where id not in (select token_id from permission_account where user_id='" . $userid . "')";
//		$sql="select * from token ";
        $restoken = fetch_array($sql);
        $data['tokendata'] = $restoken;
        $sql = "select t.id,t.name from permission_account as p,token as t where p.user_id='" . $userid . "' and p.token_id=t.id";
        $seltoken = fetch_array($sql);
        $data['seltoken'] = $seltoken;
        $this->load->view('user_edit', $data);
    }

    public function save() {
        $data['menu'] = "user_user";
        $ins_data['username'] = getGP('username');
        $ins_data['account'] = getGP('account');
        $ins_data['user_type'] = getGP('user_type');
        $ins_data['password'] = getGP('password');
        $password1 = getGP('password1');
        $ins_data['sign'] = 1;
        $ins_data['remark'] = getGP('remark');
        if (empty($ins_data['username'])) {
            alert("请输入姓名：");
        }
        if (empty($ins_data['account'])) {
            alert("请输入账号：");
        }
        if (empty($ins_data['password'])) {
            alert("请输入密码：");
        }
        if (empty($password1)) {
            alert("请输入确认密码：");
        } elseif ($password1 != $ins_data['password']) {
            alert("确认密码不一致！");
        }
//        $sel_token = $_POST['token'];
//        if (empty($sel_token)) {
//            alert("请选择要管理的公众号！");
//        }
        $this->db->insert('user', $ins_data);
        $userid = $this->db->insert_id();
        if ($userid) {
//            for ($e = 0; $e < count($sel_token); $e++) {
//                $this->db->insert('permission_account', array("user_id" => $userid, "token_id" => $sel_token[$e]));
//            }
            user_log_save($_SESSION["username"],$_SESSION["userid"],"新增或编辑管理员账号\"".$ins_data["username"]."\"");
            alert("保存成功", "index.php?c=user");
        } else {
            alert("保存失败");
        }
    }

    public function saveedit() {
        $data['menu'] = "user_user";
        $up_data['user_type'] = getGP('user_type');
        $userid = getGP('id');
        $up_data['sign'] = getGP('sign');
        $sel_token = $_POST['token'];

        $r = $this->db->update('user', $up_data, "id = $userid ");
        if ($r) {
            alert("保存成功", "index.php?c=user");
        } else {
            alert("保存失败");
        }
    }

    public function delete() {
        $data['menu'] = "user_user";
        $id = intval(getGP('id'));
        
        $res = fetch_one_array("SELECT * FROM user WHERE id=".$id);
        
        $sql = "delete from user where id='" . $id . "'";
        $r = $this->db->query($sql);
        if ($r) {
            $sql_per = "delete from permission_account where user_id='" . $id . "'";
            $this->db->query($sql_per);
            user_log_save($_SESSION["username"],$_SESSION["userid"],"删除管理员账号\"".$res["username"]."\"");
            alert("删除成功", "index.php?c=user");
        } else {
            alert("删除失败");
        }
    }

    public function check_account() {
        $account = getGP('account');
        $sql = "select * from user where account='" . $account . "'";
        $re_value['status'] = 0;
        $count = fetch_count($sql);

        if ($count > 0) {
            $re_value['status'] = 1;
        }
        echo json_encode($re_value);
    }
    
    
    public function logout() {
        $data['menu'] = "home";
        $_SESSION['username'] = "";
        $_SESSION['account'] = "";
        $_SESSION['userid'] = "";
        $_SESSION['user_type'] = "";
        $_SESSION['iduserbrand'] = "";
        $_SESSION['user_token'] = "";
        redirect('index.php');
    }

    public function set_psw() {
        $data['menu'] = "home";
        $data['account'] = $_SESSION['account'];
        $this->load->view('user_setpsw', $data);
    }

    public function psw_update() {
        $data['menu'] = "home";
        $account = getGP('account');
        $old_password = getGP('old_password');
        $ins_data['password'] = getGP('new_password');
        $password_new = getGP('new_password1');
        if (empty($old_password)) {
            alert("请输入原密码");
        }
        if (empty($ins_data['password'])) {
            alert("请输入新密码");
        }
        if (empty($password_new)) {
            alert("请输入确认新密码");
        }
        if ($password_new != $ins_data['password']) {
            alert("请输入新密码与确认密码不一致");
        }
        $sql = "select * from user where account='" . $account . "' and password='" . $old_password . "'";
        if (fetch_count($sql) <= 0) {
            alert("原密码不正确");
        } else {
            $r = $this->db->update('user', $ins_data, "account = '$account' ");
            if ($r) {
                user_log_save($_SESSION["username"],$_SESSION["userid"],"进行修改密码操作");
                alert("修改密码成功", "index.php?c=user");
            } else {
                alert("修改密码失败");
            }
        }
    }
    
    
    //显示管理员操作记录
    public function user_log(){
        $per_page = getGP('per_page') ? getGP('per_page') : 1;
        $config['per_page'] = 100; //每一页显示行数
        $limit = $config['per_page'];
        $start = ($per_page - 1) * $limit;
        
        $where = " WHERE 1 ";
        $data['menu'] = "user_log";
        $sql = "select * from admin_log $where ";
        // $sql="select t.* from permission_account as p ,permission_account as t where p.user_id='".$_SESSION['account']."' and p.token_id=t.id";
        $count = fetch_count($sql);
        $res = fetch_array($sql . " order by id desc  limit $start,$limit");
        // $res=fetch_array($sql);
        $data['res'] = $res;
        
        $config['total_rows'] = $count;
        $config['base_url'] = "index.php?c=user&m=user_log";
        $this->pagination->initialize($config);
        $page = $this->pagination->create_links();
        $data['pager'] = $page;

        $this->load->view('user_log', $data);
    }
}
